package com.woniu.customer.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.customer.pojo.CustomerAddition;
import com.woniu.customer.pojo.OrderbillingsUnit;
import com.woniu.customer.pojo.ReconsigneesUnit;
import com.woniu.customer.service.CustomerAdditionService;
import com.woniu.customer.mapper.CustomerAdditionMapper;
import com.woniu.customer.service.OrderbillingsUnitService;
import com.woniu.customer.service.ReconsigneesUnitService;
import com.woniu.pojo.PageParam;
import com.woniu.until.ResponseData;
import com.woniu.until.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;


@Service
public class CustomerAdditionServiceImpl extends ServiceImpl<CustomerAdditionMapper, CustomerAddition>
        implements CustomerAdditionService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest request;

    @Value("${snowflake.datacenterId}")
    private long datacenterId;
    @Value("${snowflake.workerId}")
    private long workerId;


    @Override
    public String addCustomer(CustomerAddition customer) {
        Long customerId = IdUtil.getSnowflake(workerId, datacenterId).nextId();
        customer.setCustomerId(customerId);
        save(customer);
        return "添加客户基本资料成功";
    }

    @Override
    public boolean updateCustomer(CustomerAddition customer) {
        LambdaUpdateWrapper<CustomerAddition> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CustomerAddition::getCustomerId, customer.getCustomerId());
        update(updateWrapper);
        return false;
    }

    @Override
    public ResponseData<?> queryAllBasic(PageParam<CustomerAddition> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        LambdaQueryWrapper<CustomerAddition> queryWrapper = new LambdaQueryWrapper<>();
        if (pageParam.getParam().getCustomerId() != null) {
            queryWrapper.eq(CustomerAddition::getCustomerId, pageParam.getParam().getCustomerId());
        }
        if (pageParam.getParam().getCustomerName() != null && pageParam.getParam().getCustomerName() != "" ) {
            queryWrapper.eq(CustomerAddition::getCustomerName, pageParam.getParam().getCustomerName());
        }

        if (pageParam.getParam().getCompanyBusiness() != null && pageParam.getParam().getCustomerName() != "") {
            queryWrapper.eq(CustomerAddition::getCompanyBusiness, pageParam.getParam().getCompanyBusiness());
        }

        if (pageParam.getParam().getWritterName() != null && pageParam.getParam().getCustomerName() != "") {
            queryWrapper.eq(CustomerAddition::getWritterName, pageParam.getParam().getWritterName());
        }
        PageInfo pageInfo = new PageInfo(list(queryWrapper));
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> addCustomerRedis(CustomerAddition customer) {

        redisTemplate.opsForHash().put(String.valueOf(customer.getWritterName()),String.valueOf(customer.getCustomerId()), JSON.toJSONString(customer));

        return new ResponseData().ok();
    }

}




